package com.sec.android.app.myfiles.facade.cmd;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.SemFwWrapper;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

import java.util.ArrayList;

public class FinishPickerCmd extends SimpleCommand {
    private static final String PICK_RESULT_FILE = "FILE";
    private static final String PICK_RESULT_URI = "FILE_URI";
    private static final String PICK_RESULT_CONTENT_TYPE = "CONTENT_TYPE";
    private static final String PICK_RESULT_DOWNLOAD_PATH = "uri";

    public enum PickerCmd {
        Done,
        Cancel
    }

    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();

        int processId = (Integer) params[0];
        AbsMyFilesFragment fragment = (AbsMyFilesFragment) params[1];
        NavigationInfo naviInfo = (NavigationInfo) params[2];
        PickerCmd pickerCmd = (PickerCmd) params[3];

        Activity activity = fragment.getActivity();
        Context context = activity.getApplicationContext();

        ArrayList<FileRecord> records = fragment.getSelectedFile();

        boolean valid;
        if (naviInfo != null && isPathPicker(naviInfo)) {
            valid = FileUtils.isValidFile(context, naviInfo.getCurRecord());
        } else {
            valid = checkFilesValid(context, records, naviInfo);
        }

        int nResultCode = Activity.RESULT_CANCELED;
        Intent result = null;

        if (valid && pickerCmd == PickerCmd.Done) {
            nResultCode = Activity.RESULT_OK;
            result = createValue(context, records, naviInfo);
        }
        activity.setResult(nResultCode, result);

        if (valid || pickerCmd == PickerCmd.Cancel) {
            activity.finish();
            NavigationManager navigationManager = NavigationManager.getInstance(processId);
            if (navigationManager != null) {
                navigationManager.clear();
            }
        }
    }

    private Intent createValue(Context context, ArrayList<FileRecord> records, NavigationInfo naviInfo) {
        Intent ret = new Intent();

        if (naviInfo.getNavigationMode() == NavigationMode.Pick_one_file) {
            setSinglePickValue(context, ret, records.get(0));
        } else if (naviInfo.getNavigationMode() == NavigationMode.Pick_files) {
            setMultiplePickValue(context, ret, records);
        } else if (naviInfo.getNavigationMode() == NavigationMode.Select_destination_path) {
            setPathPickValue(ret, naviInfo.getCurRecord());
        }
        return ret;
    }

    private void setSinglePickValue(Context context, Intent intent, FileRecord records) {
        String path = records.getFullPath();
        intent.putExtra(PICK_RESULT_FILE, path);
        intent.putExtra(PICK_RESULT_URI, FileUtils.getPickUriByPath(context, path));
        intent.putExtra(PICK_RESULT_CONTENT_TYPE, MediaFile.getMimeType(path));
        intent.setData(Uri.fromFile(SemFwWrapper.file(path)));
    }

    private void setMultiplePickValue(Context context, Intent intent, ArrayList<FileRecord> records) {
        int size = records.size();
        String[] pathList = null;
        String[] uriList = null;
        if (size > 0) {
            pathList = new String[size];
            uriList = new String[size];

            int i = 0;
            for (FileRecord record : records) {
                pathList[i] = record.getFullPath();
                Uri pickUri = FileUtils.getPickUriByPath(context, record.getFullPath());
                uriList[i] = (pickUri == null ? "" : pickUri.toString());
                i++;
            }
        } else {
            Log.e(this, "setMultiplePickValue() - Record List is null");
        }
        intent.putExtra(PICK_RESULT_URI, uriList);
        intent.putExtra(PICK_RESULT_FILE, pathList);
    }

    private void setPathPickValue(Intent intent, FileRecord records) {
        if (records != null) {
            String path = records.getFullPath();
            if (LaunchPickerCmd.isDownload()) {
                intent.putExtra(PICK_RESULT_DOWNLOAD_PATH, path);
            } else {
                intent.putExtra(PICK_RESULT_FILE, path);
            }
        }
    }

    private boolean checkFilesValid(Context context, ArrayList<FileRecord> records, NavigationInfo naviInfo) {
        boolean bRet = true;

        if (records != null && !records.isEmpty()) {
            if (records.size() <= naviInfo.getMaxSelectCnt()) {
                for (FileRecord record : records) {
                    if (!FileUtils.isValidFile(context, record)) {
                        bRet = false;
                        break;
                    }
                }
            } else {
                String msg = context.getResources().getString(R.string.max_num_selected_files, naviInfo.getMaxSelectCnt());
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                bRet = false;
            }
        } else {
            //Toast.makeText(context, context.getString(R.string.file_does_not_exist), Toast.LENGTH_SHORT).show();
            bRet = false;
        }
        return bRet;
    }

    private boolean isPathPicker(NavigationInfo naviInfo) {
        boolean bRet = false;
        if (naviInfo.getNavigationMode() == NavigationMode.Select_destination_path) {
            bRet = true;
        }
        return bRet;
    }

}
